﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HRMS.Models;
using DataManager;
using HRMS_Model;
using Lib.Web.Mvc.JQuery.JqGrid;
using System.Globalization;
using HRMS.Models.ModelBinders;

namespace HRMS.Controllers
{
    public class DegreeController : Controller
    {
        //
        // GET: /Degree/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Degrees(JqGridRequest request)
        {
            //get the count of roles
            var totalRecords = DegreesModels.GetDegreesCount();

            //Prepare JqGridData instance
            var response = new JqGridResponse
            {
                //Total pages count
                TotalPagesCount = (int)Math.Ceiling((float)totalRecords / (float)request.RecordsCount),
                //Page number
                PageIndex = request.PageIndex,
                //Total records count
                TotalRecordsCount = totalRecords
            };

            //Get Designations
            var degrees = DegreesModels.GetDegrees(request.SortingName.ToString(CultureInfo.InvariantCulture), request.SortingOrder.ToString(), request.PageIndex, request.RecordsCount, request.PagesCount.HasValue ? request.PagesCount.Value : 1);

            //Table with rows data
            foreach (var degree in degrees)
            {
                response.Records.Add(new JqGridRecord(Convert.ToString(degree.DegreeId), new List<object>() { 
                                    degree.DegreeId,
                degree.DegreeName
                }));
            }

            //Return data as json
            return new JqGridJsonResult() { Data = response };
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertDegree(Degree degreeModel)
        {
            return Json(DegreesModels.AddDegree(degreeModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateDegree([ModelBinder(typeof(DegreeModelBinders))]Degree degreeModel)
        {
            return Json(DegreesModels.UpdateDegree(degreeModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteDegree(int id)
        {
            return Json(DegreesModels.DeleteDegree(id));
        }

    }
}